package com.oalam.bookstore.domain.accounts;

import com.oalam.bookstore.domain.BusinessObject;
import com.oalam.bookstore.domain.shopping.Order;
import com.oalam.bookstore.domain.shopping.ShoppingCart;
import java.io.Serializable;

/**
 * @author Administrateur
 * @version 1.0
 * @created 28-juil.-2011 16:18:24
 */
public class Account implements Serializable, BusinessObject {

    private ShoppingCart cart = new ShoppingCart();
    private BillingInfos billingInfos;
    public Order m_Order;
    private String name = "";
    private String email = "";
    private String pwd = "";
    private int loginAttempt = 0;
    transient private boolean authentified = false;

    public Account(String email) {
        this.email = email;
    }

    public Account(String email, String pwd, String name) {
        this.name = name;
        this.email = email;
        this.pwd = pwd;
    }

    public boolean checkPassword(String pwd) {
        boolean result = false;
        if (loginAttempt++ < 3) {
            result = (this.pwd.equals(pwd));
            authentified = result;
        } else {
            // block the account
        }
        return result;
    }

    @Override
    public boolean equals(Object obj) {
        if (obj == null) {
            return false;
        }
        if (getClass() != obj.getClass()) {
            return false;
        }
        final Account other = (Account) obj;
        if ((this.name == null) ? (other.name != null) : !this.name.equals(other.name)) {
            return false;
        }
        if ((this.email == null) ? (other.email != null) : !this.email.equals(other.email)) {
            return false;
        }
        if ((this.pwd == null) ? (other.pwd != null) : !this.pwd.equals(other.pwd)) {
            return false;
        }
        return true;
    }

    @Override
    public int hashCode() {
        int hash = 3;
        return hash;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getPwd() {
        return pwd;
    }

    public void setPwd(String pwd) {
        this.pwd = pwd;
    }

    public BillingInfos getBillingInfos() {
        return billingInfos;
    }

    public void setBillingInfos(BillingInfos billingInfos) {
        this.billingInfos = billingInfos;
    }

    public ShoppingCart getShoppingCart() {
        return cart;
    }

    public boolean isAuthentified() {
        return authentified;
    }

    @Override
    public String getId() {
        return email;
    }

    @Override
    public <T extends BusinessObject> void clone(T t) {
        Account a = (Account) t;
        this.name = a.name;
        this.email = a.email;
        this.pwd = a.pwd;
        this.billingInfos = a.billingInfos;
        this.m_Order = a.m_Order;
    }

    @Override
    public String toString() {
        return "Account{" + "cart=" + cart + ", billingInfos=" + billingInfos + ", m_Order=" + m_Order + ", name=" + name + ", email=" + email + ", pwd=" + pwd + ", loginAttempt=" + loginAttempt + ", authentified=" + authentified + '}';
    }
    
    
}